#include "widget.h"
#include "ui_widget.h"
#include <QDebug>
#include <QKeyEvent>

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    ui->textEdit->installEventFilter(this);
}

Widget::~Widget()
{
    delete ui;
}

bool Widget::eventFilter(QObject *watched, QEvent *event)
{
    if(watched == ui->textEdit){
        if(event->type() == QEvent::KeyPress){
            QKeyEvent *keyEvent = (QKeyEvent*)event;
            if(keyEvent->key() == Qt::Key_Return &&  keyEvent->modifiers() == Qt::ControlModifier){
                this->sendMessage();
            }
        }
    }
    return watched->eventFilter(watched, event);
}

void Widget::on_pushButton_clicked()
{
    this->sendMessage();
}

void Widget::sendMessage()
{
    ui->listView->append(ui->textEdit->toPlainText());
    ui->textEdit->clear();
}
